﻿using System;
using System.Data;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using Common;
using Common.Entities;
using Common.Models;
using MobileSales.Interface;



namespace MobileSales.Presenter
{
    public class SheduleVisitsPresenter
    {
        private ISheduleVisitsView _view;
        private SheduleVisitsRepository _dbGV;
        private BindingSource bindingSource;

        public SheduleVisitsPresenter (ISheduleVisitsView view)
        {

            _dbGV = DataManager.GetInstance().SheduleVisits;
            bindingSource = new System.Windows.Forms.BindingSource(_dbGV.Visit, "SheduleVisits");
            _view = view;
            _view.SetBindingSource(bindingSource);

            

            view.EditRecord += EditRecord;
            view.DeleteRecord += DeleteRecord;
            view.SaveChanges += SaveChanges;
            Filter[] f = new Filter[]
                             {
                                 new Filter("Всі", "*"), 
                                 new Filter("Понеділок", "Monday"),
                                 new Filter("Вівторок", "Tuesday"), 
                                 new Filter("Середа", "Wednesday"),
                                 new Filter("Четвер", "Thursday"),
                                 new Filter("Пятниця", "Friday"), 
                                 new Filter("Субота","Saturday"), 
                                 new Filter("Неділя","Sunday"), 
                             };
            _view.SetSelectFilter(f);
            _view.SetFilter += SetFilter;

            _view.AddRecord += AddRecord;

        }

        public void AddRecord (object sender, EventArgs e)
        {
            AddVisitDialog addVisitDialog = new AddVisitDialog(true);
            addVisitDialog.OK += AddVisit;
            addVisitDialog.Cancel += CancelAddVisitDialog;
            _view.ShowAddVisitDialog(addVisitDialog);
        }

        public void CancelAddVisitDialog(object sender, EventArgs e)
        {
            _view.CloseAddDialog();
        }

        public void AddVisit(object sender, EventArgs e)
        {
            AddVisitDialog addVisitDialog = (AddVisitDialog)sender;
            DataRowView dr = (DataRowView)bindingSource.AddNew();
            BuyPoint buyPoint = ((BuyPoint) addVisitDialog.SelectedBuyPoint); 
            dr["BuyPointId"] = buyPoint.IdBuyPoint;
            dr["NameBuyPoint"] = buyPoint.NameBuyPoint;
            dr["Adress"] = buyPoint.Adress;
            Dictionary<string, bool> day = addVisitDialog.SelectDays;
            dr["Sunday"] = day["Sunday"];
            dr["Monday"] = day["Monday"];
            dr["Tuesday"] = day["Tuesday"];
            dr["Wednesday"] = day["Wednesday"];
            dr["Thursday"] = day["Thursday"];
            dr["Friday"] = day["Friday"];
            dr["Saturday"] = day["Saturday"];
            
            _view.IsChange = true;
        }

        public void EditRecord(object sender, EventArgs e)
        {
            string col = _view.ActiveColumn;
            DataRowView drv = (DataRowView)bindingSource.Current;
            bool val = !(drv[col] != DBNull.Value && drv[col].ToString() == "True");
            drv[col] = val;
            bindingSource.EndEdit();
            _view.IsChange = true;
        }

        public void SetFilter (object sender, EventArgs e)
        {
            Filter filter = _view.FilterValue;
            if (filter.Value == "*")
            {
                bindingSource.RemoveFilter();
            }
            else
                bindingSource.Filter = filter.Value + " = 1";
        }

        public void DeleteRecord (object sender, EventArgs e)
        {
            bindingSource.RemoveCurrent();
            _view.IsChange = true;
        }

        public void SaveChanges (object sender, EventArgs e)
        {
            bindingSource.EndEdit();
            _dbGV.SaveChanges();
        }
    }
}
